<template>
  <div>
    <el-form
      ref="formData"
      :model="formData"
      :rules="rules"
      label-width="80px"
      style="width: 500px"
    >
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          placeholder="请选择活动区域"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          readonly
          @click.native="drawerHandler"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <!-- 按钮 -->
      <el-form-item>
        <el-button type="primary" @click="submitHandler">确定</el-button>
        <el-button @click="closeHandler">取消</el-button>
      </el-form-item>
    </el-form>
    <!-- drawer -->
    <el-drawer
      title="我是标题"
      :visible.sync="drawer"
      :with-header="false"
      :modal="false"
    >
      <el-tree style="font-size:30px" :data="treeData" :props="{ label: 'name' }" @node-click="aaa" />
    </el-drawer>
  </div>
</template>

<script>
import EmployeeEnum from '@/constant/employees'
// 引入api方法
import { getDepartments } from '@/api/departments'
import { addEmployee } from '@/api/employees'
// 引入树形结构转换方法
import { tranListToTreeData, transArray2 } from '@/utils'

export default {
  name: 'VueAdminTemplateMasterEmpdia',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      hireType: EmployeeEnum.hireType,
      drawer: false,
      treeData: [], // 定义数组接收树形数据
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 4, message: '员工姓名为2-4个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请填写正确的手机号码',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {},
  created() {
    this.loadDepartments()
  },
  methods: {
    drawerHandler() {
      this.drawer = true
      //   this.$emit('update:visible', false)
    },
    async loadDepartments() {
      const res = await getDepartments()
      // console.log(res)
      if (res.code !== 10000) return
      // this.treeData = tranListToTreeData(res.data.depts)
      this.treeData = transArray2(res.data.depts, '')
    },
    aaa(row) {
      // console.log(row)
      this.formData.departmentName = row.introduce
    },
    submitHandler() {
      this.$refs.formData.validate(async valid => {
        if (!valid) return this.$message.error('请正确填写')
        const res = await addEmployee(this.formData)
        // console.log(res)
        if (res.code !== 10000) return
        this.$emit('update:visible', false)
        this.$emit('success')
        this.$refs.formData.resetFields()
      })
    },
    closeHandler() {
      this.$emit('update:visible', false)
      this.$refs.formData.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>

 ::v-deep .el-tree-node__label{
  font-size: 25px;
}
  ::v-deep .el-tree-node__content{
    margin: 15px 0;
  }
</style>
